<template>
  <div @scroll="rememberPos" ref="listBox" class="scroll-wrapper">
    <van-pull-refresh
      :success-text="refreshTxt"
      v-model="refreshing"
      @refresh="onRefresh"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          :to="`/article/${item.art_id}`"
          v-for="item in list"
          :key="item.art_id.toString()"
        >
          <div class="article_item">
            <h3 class="van-ellipsis">{{ item.title }}</h3>
            <div
              class="img_box"
              v-if="item.cover.images && item.cover.images.length"
            >
              <van-image
                lazy-load
                :key="i"
                v-for="(img, i) in item.cover.images"
                :class="item.cover.type === 1 ? 'w100' : 'w33'"
                fit="cover"
                :src="img"
              >
                <template v-slot:loading>
                  <van-loading type="spinner" size="20" />
                </template>
                <template v-slot:error>加载失败</template>
              </van-image>
              <!-- <van-image class="w33" fit="cover" src="https://img.yzcdn.cn/vant/cat.jpeg" />
              <van-image class="w33" fit="cover" src="https://img.yzcdn.cn/vant/cat.jpeg" />-->
            </div>
            <div class="info_box">
              <span>{{ item.aut_name }}</span>
              <span>{{ item.comm_count }}评论</span>
              <span>{{ item.pubdate | relTime }}</span>
              <span
                v-if="$store.state.user"
                @click.stop="openMore(item.art_id.toString())"
                class="close"
              >
                <van-icon name="cross"></van-icon>
              </span>
            </div>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
    <!-- 更多操作弹出层 -->
    <MoreAction
      v-if="$store.state.user"
      v-model="showMore"
      @on-dislike="removeArticle"
      @on-report="removeArticle"
      :articleId="articleId"
    />
  </div>
</template>

<script>
import { getArticlesById } from '@/api/article'
import MoreAction from './components/more-action'
export default {
  props: {
    channelId: {
      type: Number,
      default: 0
    }
  },
  components: {
    MoreAction
  },
  // 处理登录后的，缓存问题=》重新刷新
  // watch: {
  //   '$store.state.user.refresh_token' () {
  //     console.log('refresh list...')
  //     this.refreshing = true
  //     this.onRefresh()
  //   }
  // },
  // keeplive下组件激活时调用
  activated () {
    // console.log('in', this.channelId)
    this.scrollList()
  },
  // deactivated () {
  //   console.log('out', this.channelId)
  // },
  data () {
    return {
      // 文章列表
      list: [],
      // 加载状态
      loading: false,
      // 加载完成
      finished: false,
      // 是否正在刷新
      refreshing: false,
      // 分页时间戳
      timestamp: +new Date(),
      // 刷新后提示
      refreshTxt: '',
      // 控制浮层显示
      showMore: false,
      // 当前选中文章的ID
      articleId: ''
    }
  },
  methods: {
    // 删除文章
    removeArticle () {
      // 获取要删除文章ID的索引
      const index = this.list.findIndex((item) => item.art_id.toString() === this.articleId)
      // debugger
      // 删除当前不感兴趣文章
      this.list.splice(index, 1)
    },
    // 打开更多浮层
    openMore (articleId) {
      // console.log(articleId, this.showMore)
      this.showMore = true
      this.articleId = articleId
    },
    // 记录列表滚动位置
    rememberPos (e) {
      // 存储到实例this上
      this._scrollTop = e.target.scrollTop
      // console.log(e.target.scrollTop)
    },
    // 根据记录滚动到上次浏览位置
    scrollList () {
      // eslint-disable-next-line no-prototype-builtins
      if (this.hasOwnProperty('_scrollTop')) {
        this.$nextTick(() => {
          this.$refs.listBox.scrollTop = this._scrollTop
        })
      }
    },
    // 加载列表执行
    async onLoad () {
      // await this.$sleep()
      // 异步更新数据
      const { status, data } = await getArticlesById(this.channelId, this.timestamp)
      if (status === 200) {
        // 追加列表数据
        this.list.push(...data.results)
        // 加载状态结束
        this.loading = false
        // 刷新完成
        this.refreshing = false
        this.refreshTxt = '刷新成功'
        // 数据全部加载完成
        if (data.pre_timestamp) {
          this.timestamp = data.pre_timestamp
        } else {
          this.finished = true
        }
      }
    },
    // 刷新执行
    onRefresh () {
      // 清空列表数据
      if (this.refreshing) {
        this.list = []
      }
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.finished = false
      this.loading = true
      // 设置最新的时间戳
      this.timestamp = Date.now()
      this.onLoad()
    }
  }
}
</script>

<style lang="less" scoped>
// 列表item样式
.article_item {
  h3 {
    font-weight: normal;
    line-height: 2;
  }
  .img_box {
    display: flex;
    justify-content: space-between;
    .w33 {
      width: 33%;
      height: 90px;
    }
    .w100 {
      width: 100%;
      height: 180px;
    }
  }
  .info_box {
    color: #999;
    line-height: 2;
    position: relative;
    font-size: 12px;
    span {
      padding-right: 10px;
      &.close {
        border: 1px solid #ddd;
        border-radius: 2px;
        line-height: 15px;
        height: 12px;
        width: 16px;
        text-align: center;
        padding-right: 0;
        font-size: 8px;
        position: absolute;
        right: 0;
        top: 7px;
      }
    }
  }
}
</style>
